package com.example.emailWeb.Socket.Proxy;

import com.example.emailWeb.Socket.test.sendObject.DataBuff;
import com.example.emailWeb.ntru.encrypt.NtruProxyParameters;
import com.example.emailWeb.ntru.encrypt.NtruProxyUserB;
import com.example.emailWeb.ntru.polynomial.IntegerPolynomial;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class RequesterDemo {
    /**
     * 1. 给文件请求者发送data
     * 2. 接收a，生成b，把b发给服务器
     *
     * @param args
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Socket socket = new Socket(IpPort.FileOwnerIP, IpPort.FileOwnerPort);
        //发送数据给文件拥有者
        PolySocketData polySocketData1 = send(socket, "红楼梦", "tiffany3344@163.com", "tiffany33440@163.com", 1);
        //接收文件拥有者返回的data,收到a
        PolySocketData polySocketData = recPolyData(socket);
        System.out.println(polySocketData);

        //生成b，把b发给服务器
        NtruProxyUserB UserB = new NtruProxyUserB(NtruProxyParameters.APR2011_439);
        ProxyProcess proxyProcess = new ProxyProcess(UserB);
        if(polySocketData.a != null){
            proxyProcess.a = polySocketData.a;
        }
        proxyProcess.Main(polySocketData);
        socket.close();

        PolySocketData cbFromServer = getCBFromServer(polySocketData1);

        Scanner scanner = new Scanner(System.in);
        String str = "";
        ObjectOutputStream os = null;
        ObjectInputStream ois = null;
        PolySocketData polySocketData2 = null;
        PolySocketData polySocketData3 = null;
        Socket socket2 = null;
        while (true) {
            str = scanner.nextLine();
            if (str.equalsIgnoreCase("CB")) {

                socket2 = new Socket(IpPort.ServerIP, IpPort.ServerPort);
                os = new ObjectOutputStream(socket2.getOutputStream());
                polySocketData2 = new PolySocketData(polySocketData);
                polySocketData2.requester = true;
                polySocketData2.encry = true;
                os.writeObject(polySocketData2);

                ois = new ObjectInputStream(socket2.getInputStream());
                polySocketData3 = (PolySocketData) ois.readObject();

                String s = UserB.decrypt(polySocketData3.CB);
                System.out.println(s);
                    /*if (os != null)
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    if(ois!=null){
                        try {
                            ois.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (socket != null)
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }*/
            }
        }


//        String s = UserB.decrypt(cbFromServer.CB);

    }

    /**
     * 发完data之后，不要关闭socket，还等着FileOwner回复
     *
     * @param fileName
     */
    public static PolySocketData send(Socket socket, String fileName, String fileRequesterEmail,String fileOwnerEmail,int keyOrder) {
        PolySocketData polySocketData = new PolySocketData(fileName, fileRequesterEmail,fileOwnerEmail,keyOrder);
        ObjectOutputStream os = null;
        try {
            os = new ObjectOutputStream(socket.getOutputStream());
            os.writeObject(polySocketData);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            /*if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }*/
        }
        return polySocketData;
    }

    /**
     * 收到回复了，关闭socket
     * @param socket
     * @return
     * @throws IOException
     */
    public static PolySocketData recPolyData(Socket socket) throws IOException {
        ObjectInputStream ois = null;
        PolySocketData polySocketData = null;
        try {
            ois = new ObjectInputStream(socket.getInputStream());
            polySocketData = (PolySocketData) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ois!=null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(socket!=null)
                socket.close();
        }
        return polySocketData;
    }

    public static PolySocketData getCBFromServer(PolySocketData polySocketData) throws IOException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        String str = "";
        ObjectOutputStream os = null;
        ObjectInputStream ois = null;
        PolySocketData polySocketData2 = null;
        Socket socket = null;
        while (true) {
            str = scanner.nextLine();
            if (str.equalsIgnoreCase("CB")) {

                socket = new Socket(IpPort.ServerIP, IpPort.ServerPort);
                os = new ObjectOutputStream(socket.getOutputStream());
                PolySocketData polySocketData1 = new PolySocketData(polySocketData);
                polySocketData1.requester = true;
                polySocketData1.encry = true;
                os.writeObject(polySocketData1);

                ois = new ObjectInputStream(socket.getInputStream());
                polySocketData2 = (PolySocketData) ois.readObject();
                if (polySocketData2.CB != null) {
                    break;
                }

                    /*if (os != null)
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    if(ois!=null){
                        try {
                            ois.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (socket != null)
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }*/
            }
        }
        return polySocketData2;
    }
}
